using UnityEngine;
using System.Collections;

/// \class  gkTestPerformanceEventHandler
/// \brief  Test the performance of event handler
[AddComponentMenu("GK/Test/Performance/Message/gkTestPerformanceEventHandler")]
[RequireComponent(typeof(gkEventComponent))]
public class gkTestPerformanceEventHandler : MonoBehaviour
{	
	/// The key to launch the test
	public KeyCode LaunchTestKey = KeyCode.Q;
	
	/// The number of test loops
	public int NbTestLoops = 1000;
	
	/// The event component
	private gkEventComponent m_rEventComponent;
	
	/// \brief  Called just after the game begin to play
	void Start()
	{
		// Grab the event component
		m_rEventComponent = GetComponent<gkEventComponent>();
	}
	
	/// \brief  Called each frame
	void Update()
	{
		if(Input.GetKeyDown(LaunchTestKey))
		{
			// Launch the test
			TestPerformance();
		}
	}
	
	/// \brief  The Test
	void TestPerformance()
	{
		print("EventHandlingDuration : " + NbTestLoops + " Calls = " + TimeEventHandling() + " ms");
	}
	
	/// \brief  Time send message
	float TimeEventHandling()
	{
		float fSendMessageBeginTime;
		float fSendMessageEndTime;
		
		fSendMessageBeginTime = Time.realtimeSinceStartup * 1000.0f;
		
		// launch the send messages
		for(int i=0; i<NbTestLoops; i++)
		{
			m_rEventComponent.RaiseEvent("ReceiveMessage");
		}
		
		fSendMessageEndTime = Time.realtimeSinceStartup * 1000.0f;
		
		return fSendMessageEndTime - fSendMessageBeginTime;
	}
}